package cn.lzb.book.server.api;

import cn.lzb.book.server.common.exception.BaseException;
import cn.lzb.book.server.entity.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author liuzhibo
 * @date 2024/8/28 16:29
 **/
@Slf4j
@RestControllerAdvice
public class ExceptionAdvice {

    @ExceptionHandler(Exception.class)
    public BaseResult handleException(Exception e) {
        if (log.isErrorEnabled()) {
            log.error("handleException", e);
        }
        if (e instanceof BaseException) {
            BaseException baseException = (BaseException) e;
            return BaseResult.failure(baseException);
        } else {
            return BaseResult.failure(e);
        }
    }

}
